﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Utils;

namespace HowHigh
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MainPage_Loaded);
            DataContext = App.Current.AppInfo;
        }

        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (App.Current.Gps == null)
            {
                App.Current.Gps = new Utils.Gps.Location();
                App.Current.Gps.Start();
            }
            Model.ModelHelper.DeleteOldProjects();
            listBox1.ItemsSource = Model.DBDataContext.Default.TowPointProjet.OrderByDescending(i => i.LastUpdate).ToList();
            listBox1.SelectedIndex = -1;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/AllProjects.xaml", UriKind.Relative));
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            App.Current.CurrentProject = null;
            NavigationService.Navigate(new Uri("/TestPage.xaml", UriKind.Relative));
        }

        private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var p = listBox1.SelectedItem as Model.Project;
            App.Current.CurrentProject = p;
            if (p != null)
                NavigationService.Navigate(new Uri("/TestPage.xaml", UriKind.Relative));
        }

        private void button1_Click_1(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/About.xaml", UriKind.Relative));
        }
    }
}